//
//  FinddetailViewController.m
//  
//
//  Created by 郭明皓 on 15/10/24.
//
//

#import "FinddetailViewController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "GMHFindmodel.h"
#import "TuijianCell.h"
#import "FindThirdViewController.h"
#import "GMHDataFiled.h"
#import "GMH_Share.h"
@interface FinddetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain )UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *detailArr;
@property (nonatomic, retain)UIView *header;

@end

@implementation FinddetailViewController
- (void)dealloc
{
    [_header release];
    [_tableView release];
    [_detailArr release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 49) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100;
    
    self.header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 310)];
    self.tableView.tableHeaderView = self.header;
    [self.header release];
//    self.header.backgroundColor = [UIColor yellowColor];
    
    
    
    [self create];
}
- (void)create
{
    [GMHDataFiled isHaveFileDocument];
    [GMHDataFiled isHaveFileFind];
    NSInteger number = [self.tagId integerValue];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *strurl = [NSString stringWithFormat:@"http://apiphp.yaochufa.com/portal/theme/themeinfo?system=android&pageIndex=1&cityCode=210200&channel=xiaomimarket&pageSize=20&imei=ffffffff-9317-02f2-ffff-fffff60c2478&regId=060dc37cc2d&id=%ld&version=4.3.1&deviceToken=ffffffff-9317-02f2-ffff-fffff60c2478", number];
    [manager GET:strurl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSString *image = dic[@"content"][@"imageUrl"];
        self.detailArr = [GMHFindmodel baseModelArry:dic[@"content"][@"productList"]];
        
        
        UIImageView *images = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, self.tableView.frame.size.width - 10, 200)];
        [self.header addSubview:images];
        [images release];
        [images sd_setImageWithURL:[NSURL URLWithString:image]placeholderImage:[UIImage imageNamed:@"travel.jpg"]];
        
        UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 210, self.tableView.frame.size.width - 10, 20)];
        [self.header addSubview:titlelabel];
        [titlelabel release];
        titlelabel.text = dic[@"content"][@"subTitle"];
        
        UILabel *detail = [[UILabel alloc]initWithFrame:CGRectMake(5, 230, self.tableView.frame.size.width - 10, 60)];
        [self.header addSubview:detail];
        [detail release];
        detail.font = [UIFont systemFontOfSize:12];
        detail.text = dic[@"content"][@"description"];
        detail.numberOfLines = NSIntegerMax;
        
        UILabel *area = [[UILabel alloc]initWithFrame:CGRectMake(self.tableView.frame.size.width - 60, 290, 50, 20)];
        [self.header addSubview:area];
        area.font = [UIFont systemFontOfSize:12];
        [area release];
        area.text = [NSString stringWithFormat:@"%@", dic[@"content"][@"districtName"]];
        area.textAlignment = NSTextAlignmentRight;
        
        self.title = dic[@"content"][@"title"];
        
        
        if ([GMH_Share SingleShare].isNight == YES) {
            self.view.backgroundColor = [UIColor blackColor];
            self.header.backgroundColor = [UIColor blackColor];
            images.alpha = 0.4;
            titlelabel.backgroundColor = [UIColor darkGrayColor];
            detail.backgroundColor = [UIColor darkGrayColor];
        }
        else
        {
            
        }
        
        [self.tableView reloadData];
        
        
        NSString *sendBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        NSString *CachesPath = [sendBoxPath stringByAppendingPathComponent:@"Travel/Find/title.plist"];
        [NSKeyedArchiver archiveRootObject:dic toFile:CachesPath];
        NSLog(@"%@", CachesPath);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"详情失败");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.detailArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *strs = @"tableView";
    TuijianCell *cell = [tableView dequeueReusableCellWithIdentifier:strs];
    if (!cell) {
        cell = [[TuijianCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strs];
    }
    if (self.detailArr.count != 0) {
        GMHFindmodel *find = self.detailArr[indexPath.row];
        [cell.leftimageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://cdn5.jinxidao.com/%@", find.url]]placeholderImage:[UIImage imageNamed:@"travel.jpg"]];
        cell.title.text = find.productName;
        cell.describe.text = [NSString stringWithFormat:@"[%@]%@", find.cityName, find.productTitleContent];
        cell.money.text = [NSString stringWithFormat:@"￥%@",find.price];
        cell.normalMoney.text = [NSString stringWithFormat:@"￥%@",find.retailPrice];
        cell.sell.text = [NSString stringWithFormat:@"已售%@", find.saledCount];
        // 字体删除线
        NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@", find.retailPrice]];
        NSRange contentRange = {0.9,[content length]};
        [content addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleThick] range:contentRange];
        cell.normalMoney.attributedText = content;
    }
    
    return  cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     FindThirdViewController *detail = [[FindThirdViewController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    GMHFindmodel *find = self.detailArr[indexPath.row];
    detail.productIds = find.productId;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
